<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\library\Sms as Smslib;
use app\common\model\User;
use think\Hook;
use think\Db;
use app\api\model\Boxfl;

class Box extends Api
{
    /**
     * @var array 前置操作方法列表
     */
    protected $beforeActionList = [];

    /**
     * 无需登录的方法,同时也就不需要鉴权了
     * @var array
     */
    protected $noNeedLogin = ['*'];

    /**
     * 无需鉴权的方法,但需要登录
     * @var array
     */
    protected $noNeedRight = [];

    /**
     * 权限Auth
     * @var Auth 
     */
    protected $auth = null;
    /**
     * 获取盲盒信息
     */
    public function getBox()
    {
        $where = [];
        if ($this->auth->id){
            //查询新人专享盲盒
            $boxIds = Boxfl::where("new_switch",1)->column("id");
            $boxArr = Db::table("box_order")->where(["user_id"=>$this->auth->id,"boxfl_id"=>['in',$boxIds],"status"=>"used"])->group("boxfl_id")->column("boxfl_id");
            $where['id'] = ["not in",$boxArr];
        }
        $where['boxswitch'] = 1;
        $box = Boxfl::where($where)->order('weigh desc')->select();

        if (empty($box)) {
            $this->error('未找到盲盒');
        } else {
            //如果是登录用户查询是否买过新人专享盲盒，买过前端就不展示

            foreach ($box as &$box_v) {
                $box_v['box_banner_images'] = cdnurl($box_v['box_banner_images'], true);
                $box_v['box_foot_images'] = cdnurl($box_v['box_foot_images'], true);
            }
            $this->success('盲盒数据', $box);
        }
    }
    
    /**
     * 首页获取盲盒信息
     */
    public function getBoxIndex()
    {
        $where = [];
        if ($this->auth->id){
            //查询新人专享盲盒
            $boxIds = Boxfl::where("new_switch",1)->column("id");
            $boxArr = Db::table("box_order")->where(["user_id"=>$this->auth->id,"boxfl_id"=>['in',$boxIds],"status"=>"used"])->group("boxfl_id")->column("boxfl_id");
            $where['b.id'] = ["not in",$boxArr];
        }
        $where['b.boxswitch'] = 1;
        $lable = $this->request->post("lable");
        if($lable){
            $where['b.lable'] = $lable;
        }
         $boxes = Db::table('box_boxfl')
            ->alias('b')
            ->field('b.*, MAX(g.goods_pirce) as max_price, MIN(g.goods_pirce) as min_price')
            ->join('box_mhgoods g', 'b.id = g.boxfl_id')
            ->where($where)
            ->group('b.id')
            ->order('b.weigh desc')
            ->select();
             // 查询每个盲盒下前四个商品的图片信息
        foreach ($boxes as &$box) {
            $goods = Db::table('box_mhgoods')
                ->where('boxfl_id', $box['id'])
                ->field('id, goods_images')
                ->order("goods_pirce desc")
                ->limit(4)
                ->select();
            $box['goods'] = $goods;
        }
            
        if (empty($boxes)) {
            $this->error('未找到盲盒');
        } else {
            //如果是登录用户查询是否买过新人专享盲盒，买过前端就不展示
            foreach ($boxes as &$box_v) {
                $box_v['max_price'] = (int)$box_v['max_price'];
                $box_v['min_price'] = (int)$box_v['min_price'];
            }
            $this->success('盲盒数据', $boxes);
        }
    }

    /**
     * 获取盲盒推荐图-权重排序
     */
    public function getBoxW()
    {
        $box = Boxfl::getBoxflW();
        if (empty($box)) {
            $this->error('未找到盲盒');
        } else {
            foreach ($box as &$box_v) {
                $box_v['box_thumb_image'] = cdnurl($box_v['box_thumb_image'], true);
            }
            $this->success('盲盒数据', $box);
        }
    }

    /**
     * 查询盲盒商品
     */
    public function getBoxShop()
    {
        //获取盲盒ID
        $id = input('id');
        //         print_r($id);
        // exit;
        //根据ID查询对应盲盒
        $boxfl = Db::table('box_boxfl')->where('boxswitch', 1)->where('id', $id)->find();
        
        //查询对应商品
        $shops = Db::table('box_mhgoods')->where('boxfl_id', $id)->order('luckycoin desc')->select();
        // print_r($shops);
        
        //获取所有商品的概率计算总基数
        $prizeRate = array_column($shops, 'probability');
        $total = array_sum($prizeRate);
        
        
        
        $boxfl['box_banner_images'] = cdnurl($boxfl['box_banner_images'], true);
        $boxfl['box_foot_images'] = cdnurl($boxfl['box_foot_images'], true);
        if(!$shops){
            $this->error('该盲盒没有商品');
        }
        
        
        
          $tag_probability=[
            'normal'=>0,
            'rare'=>0,
            'supreme'=>0,
            'legend'=>0,
            
            ];

        foreach ($shops as &$shop) {
            $shop['images'] = explode(',', $shop['goods_images']);
            // $shop['goods_image'] = cdnurl($shop['goods_image'], true);
            $shop['image'] = cdnurl($shop['images'][0], true);
                    //bcscale(7);
           // $shop['calculate_probability']=bcdiv($shop['probability'],$total);
             //计算当前商品的中奖概率
              $shop['calculate_probability']=bcdiv($shop['probability'],$total,8);
              if($shop['tag']=='normal'){
                  $tag_probability['normal']= bcadd($tag_probability['normal'],$shop['calculate_probability'],8);
              }
               if($shop['tag']=='rare'){
                  $tag_probability['rare']= bcadd($tag_probability['rare'],$shop['calculate_probability'],8);
              }
               if($shop['tag']=='supreme'){
                  $tag_probability['supreme']= bcadd($tag_probability['supreme'],$shop['calculate_probability'],8);
              }
               if($shop['tag']=='legend'){
                  $tag_probability['legend']= bcadd($tag_probability['legend'],$shop['calculate_probability'],8);
                   
               }
            
            
            
            
            
        }
        $znum = Db::table('box_mhgoods')->where('boxfl_id', $id)->count();
        $ssnum = Db::table('box_mhgoods')->where('boxfl_id', $id)->where('tag', 'rare')->count();
        $zgprice = Db::table('box_mhgoods')->where('boxfl_id', $id)->order('goods_pirce DESC')->select();
        $zdprice = Db::table('box_mhgoods')->where('boxfl_id', $id)->order('goods_pirce ASC')->select();
        // print_r($zgprice);
        // exit;
  $this->success('查询成功', ['mh' => $boxfl, 'shop' => $shops, 'znum' => $znum, 'xynum' => $ssnum, 'zg' => $zgprice[0]['goods_pirce'], 'zd' => $zdprice[0]['goods_pirce'],'tag_probability'=>$tag_probability]);
    }
    /**
     * 查看每个商品详情
     */
    public function getShop()
    {
        //获取商品ID
        $id = input('id');
        $shop = Db::table('box_mhgoods')->where('id', $id)->find();
        if (empty($shop)) {
            $this->error('未找到该商品');
        }
        $image = explode(',', $shop['goods_images']);
        // $shop['image'] = cdnurl($image[0], true);
        foreach ($image as &$image_v) {
            $shop['image'][] = cdnurl($image_v, true);
        }
        $this->success('查询成功', $shop);
    }
    /**
     * 获取连抽优惠
     */
    public function getlcyh()
    {
        $s = Db::table('box_setting')->where('id', 1)->find();
        $this->success('优惠比例', $s['lcyhbl'] / 100);
    }
    /*
    获取盲盒商品
    */
    public function getBoxs()
    {
        //获取盲盒ID
        $id = input('id');
        //查询对应商品
        $shops = Db::table('box_mhgoods')->where('boxfl_id', $id)->order('goods_pirce DESC')->select();
        // print_r($shops);
        $cs = [];
        $ss = [];
        $xy = [];
        $gj = [];

        foreach ($shops as &$shop) {
            $shop['images'] = explode(',', $shop['goods_images']);
            // $shop['goods_image'] = cdnurl($shop['goods_image'], true);
            $shop['image'] = cdnurl($shop['images'][0], true);
        }
        foreach ($shops as $shop_v) {
            if ($shop_v['tag'] == 'normal') {
                $gj[] = $shop_v;
            } else if ($shop_v['tag'] == 'rare') {
                $xy[] = $shop_v;
            } else if ($shop_v['tag'] == 'supreme') {
                $ss[] = $shop_v;
            } else if ($shop_v['tag'] == 'legend') {
                $cs[] = $shop_v;
            }
        }
        $arr = [
            'gj' => $gj,
            'xy' => $xy,
            'ss' => $ss,
            'cs' => $cs,
        ];
        $this->success('商品', $arr);
    }
}
